package com.scwatch.ext.web.back;

import com.scwatch.common.web.Servlets;
import com.scwatch.core.constant.Constants;
import com.scwatch.core.domain.PushMsg;
import com.scwatch.core.domain.User;
import com.scwatch.core.service.PushMsgService;
import com.scwatch.core.support.Context;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;

import java.util.Map;

import static com.scwatch.core.constant.Constants.MESSAGE;
import static com.scwatch.core.constant.Constants.SAVE_SUCCESS;

@Controller
@RequestMapping("/ext/push")
public class JpushController {
    private static final Logger logger = LoggerFactory.getLogger(JpushController.class);

    @Autowired
    private PushMsgService pushMsgService;

    @RequiresPermissions("ext:push:save")
    @RequestMapping("save.do")
    public String save(HttpServletRequest request, PushMsg bean, String redirect,
                       RedirectAttributes ra) {
        Integer siteId = Context.getCurrentSiteId();
        Integer userId = Context.getCurrentUserId();
        /*----------------[ 推送 文字，新闻  url 地址]-------------*/


        ra.addFlashAttribute(MESSAGE, SAVE_SUCCESS);
        if (Constants.REDIRECT_LIST.equals(redirect)) {
            return "redirect:list.do";
        } else {
            return "redirect:create.do";
        }
    }

    @RequiresPermissions("ext:push:delete")
    @RequestMapping("delete.do")
    public String delete(HttpServletRequest request, Integer[] ids,
                         RedirectAttributes ra) {
        Integer siteId = Context.getCurrentSiteId();
        Integer userId = Context.getCurrentUserId();
        PushMsg[]  list =   pushMsgService.delete(ids);
        for (PushMsg msg : list){
            logger.info("删除推送信息："+msg.getTitle());
        }
        ra.addFlashAttribute(MESSAGE, SAVE_SUCCESS);
        return "redirect:list.do";
    }

    @RequiresPermissions("core:push:create")
    @RequestMapping("create.do")
    public String create(Integer id, org.springframework.ui.Model modelMap) {

        /*------[ 获取发送类型  如：新闻、视频、url]--------------*/

        return "ext/push_msg/push_msg_form";
    }


    @RequiresPermissions("ext:push:list")
    @RequestMapping("list.do")
    public String list(@PageableDefault(sort = "id", direction = Sort.Direction.DESC) Pageable pageable,
                       HttpServletRequest request, org.springframework.ui.Model modelMap) {
        Integer siteId = Context.getCurrentSiteId();
        User user = Context.getCurrentUser();
        Map<String, String[]> params = Servlets.getParamValuesMap(request,
                Constants.SEARCH_PREFIX);
        Page<PushMsg> pagedList = pushMsgService.findAll(siteId, params, pageable);
        modelMap.addAttribute("pagePushList", pagedList);
        return "ext/push_msg/push_msg_list";
    }


}
